/*
 *
 * =========================================================================
 *
 * Copyright (c) 2009-2010 Sperasoft, Inc. All rights reserved.
 * This software is the confidential and proprietary information of Sperasoft, Inc.
 * You shall not disclose such Confidential Information and shall use it only in
 * accordance with the terms of license agreement you entered into with Sperasoft, Inc.
 *
 * =========================================================================
 *
 * For more information, please see http://sperasoft.com
 *
 */
package org.shagin.jfin.core;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * The Class TextFileStrings.
 */
public class TextFileStrings {

	/** The Constant log. */
	public static final Logger log = LoggerFactory
			.getLogger(TextFileStrings.class);

	/**
	 * Checks if is comment.
	 * 
	 * @param s
	 *            the s
	 * 
	 * @return true, if is comment
	 */
	public static boolean isComment(String s) {
		return (StringUtils.isNotEmpty(s) && s.startsWith("//"));
	}

	/**
	 * Gets the.
	 * 
	 * @param bufferedReader
	 *            the buffered reader
	 * @param ignoreComments
	 *            the ignore comments
	 * 
	 * @return the list< string>
	 */
	public static List<String> get(BufferedReader bufferedReader,
			boolean ignoreComments) {
		Debug.traceEnter(log);
		List<String> result = new LinkedList<String>();
		String line;
		try {
			while ((line = bufferedReader.readLine()) != null) {

				if (!(ignoreComments && isComment(line))) {
					log.trace("TextFileStrings.get(): '" + line + "'");
					result.add(line);
				}
			}
		} catch (IOException e) {
			Debug.error(log, e);
		}
		return result;
	}

	/**
	 * Gets the.
	 * 
	 * @param inputStream
	 *            the input stream
	 * @param ignoreComments
	 *            the ignore comments
	 * 
	 * @return the list< string>
	 */
	public static List<String> get(InputStream inputStream,
			boolean ignoreComments) {
		Debug.traceEnter(log);
		return get(new BufferedReader(new InputStreamReader(inputStream)),
				ignoreComments);
	}

	/**
	 * Gets the.
	 * 
	 * @param fileName
	 *            the file name
	 * @param ignoreComments
	 *            the ignore comments
	 * 
	 * @return the list< string>
	 * 
	 * @throws FileNotFoundException
	 *             the file not found exception
	 */
	public static List<String> get(String fileName, boolean ignoreComments)
			throws FileNotFoundException {
		Debug.traceEnter(log);
		return get(new FileInputStream(fileName), ignoreComments);
	}

	/**
	 * Gets the.
	 * 
	 * @param file
	 *            the file
	 * @param ignoreComments
	 *            the ignore comments
	 * 
	 * @return the list< string>
	 * 
	 * @throws FileNotFoundException
	 *             the file not found exception
	 */
	public static List<String> get(File file, boolean ignoreComments)
			throws FileNotFoundException {
		return get(new FileInputStream(file), ignoreComments);
	}
}
